﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Robot.SearchNearest
{
    public partial class frmMain : Form
    {
        StateHandler handler = new StateHandler();

        public frmMain()
        {
            InitializeComponent();
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            handler.CurrentState = new SearchState(nxtBrick, 20, 15);
        }

        private void UpdateBatteryLevel()
        {
            timerBatteryLevel.Enabled = true;
            int level = nxtBrick.Comm.GetBatteryLevel();
            level = level / 100;
            lbBatteryLevel.Text = "Battery level: " + (level / 10) + "." + (level % 10) + "V";
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            nxtBrick.Connect();
            UpdateBatteryLevel();
            handler.Logger = new ListBoxLogger(lsLog);
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            handler.CurrentState = new StoppedState(nxtBrick);
        }

        private void nxtSonar_ValueChanged(Bram.NxtSharp.NxtSensor sensor)
        {
            if (nxtSonar.RawValue < 8)
            {
                handler.CurrentState = new StoppedState(nxtBrick);
            }
        }
    }
}
